<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input v-model="query.taskName" class="filter-item" style="width: 200px" placeholder="请输入任务名称" clearable />
        <el-select v-model="query.status" class="filter-item" placeholder="请选择迁移状态" filterable clearable style="width:200px">
          <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!-- 功能按钮 -->
      <crudOperation :permission="permission" />
    </div>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="crud.loading"
      style="width: 100%;"
      :data="crud.data"
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column v-if="columns.visible('taskName')" prop="taskName" label="任务名称" />
      <el-table-column v-if="columns.visible('transferType')" prop="transferType" label="数据迁移方式">
        <template slot-scope="scope">
          {{ formatterTransferType(scope.row.transferType) }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('status')" label="迁移状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'TODO'" type="info">待迁移</el-tag>
          <el-tag v-if="scope.row.status === 'PROCESSING'">迁移中</el-tag>
          <el-tag v-if="scope.row.status === 'FINISH'" type="success">完成</el-tag>
          <el-tag v-if="scope.row.status === 'FAIL'" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('total')" prop="total" width="305px" align="center" label="迁移进度">
        <template slot-scope="scope">
          <el-tag type="info">总数：{{ scope.row.total }}</el-tag>
          <el-tag type="success" style="margin: 0px 5px">迁移成功: {{ scope.row.successCount }}</el-tag>
          <el-tag type="danger">迁移失败: {{ scope.row.failCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="开始时间" width="180">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.startTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="结束时间" width="180">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.endTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="详情" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toShowDetail(scope.row.pkId)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <!-- 新增页面 -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="1200px"
      top="5vh"
    >
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="150px">
        <basic-info ref="basicInfo" v-model="form" :dialog-type="dialogType" />
        <div class="dialog-footer" style="text-align: center;margin-top: 10px;">
          <el-button @click="crud.cancelCU">关 闭</el-button>
          <el-button type="primary" @click="saveOnly">保存</el-button>
        </div>
      </el-form>
    </el-dialog>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showDataTransferLogFlag"
      title="数据迁移任务详情"
      width="1400px"
      top="5vh"
    >
      <data-transfer-log v-if="showDataTransferLogFlag" :data-transfer-task-pk-id="dataTransferTaskPkId" />
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/expand/data/transfer/CRUD.operation'
import pagination from '@crud/Pagination'
import dataTransferTaskApi from '@/api/data/transfer/dataTransferTask'
import validateTaskName from '@/utils/taskNameRepeat'
import basicInfo from './components/basicInfo/index'
import dataTransferLog from './components/log/index'
import dataTransferList from '@/api/data/transfer/dataTransferList'
import dataSourceConnApi from '@/api/metadata/dataSource'

const defaultCrud = CRUD({ title: '数据迁移任务', url: '/api/data/transfer/task', crudMethod: { ...dataTransferTaskApi }})
const defaultForm = {
  pkId: null,
  taskName: null,
  tableSelectType: '直连',
  dataSyncType: 'TODAY',
  dataTransferType: 'Jdbc',
  sourceConnPkId: null,
  database: null,
  dataSyncStartTime: null,
  dataSyncEndTime: null,
  status: 'TODO',
  total: 0,
  successCount: 0,
  failCount: 0,
  isDataAnalysis: 'Y',
  tableList: []
}
export default {
  components: { crudOperation, rrOperation, pagination, basicInfo, dataTransferLog },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'dataDownsize:add'],
        edit: ['admin', 'dataDownsize:edit'],
        del: ['admin', 'dataDownsize:del']
      },
      statusOptions: [
        { key: 'TODO', label: '待迁移' },
        { key: 'PROCESSING', label: '迁移中' },
        { key: 'FINISH', label: '完成' },
        { key: 'FAIL', label: '失败' }
      ],
      showDataTransferLogFlag: false,
      dataTransferTaskPkId: null,
      dataTransferType: 'Jdbc',
      taskPkId: '',
      dialogType: '',
      rules: {
        taskName: [
          { required: true, validator: validateTaskName.bind(this, dataTransferTaskApi, this.form), trigger: 'blur' }
        ],
        tableSelectType: [
          { required: true, message: '请选择表选择方式', trigger: 'change' }
        ],
        sourceConnPkId: [
          { required: true, message: '请选择服务名称', trigger: 'change' }
        ],
        database: [
          { required: true, message: '请选择数据库名称', trigger: 'change' }
        ],
        dataSyncType: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        customTime: [
          { required: true, message: '请选择自定义日期', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 数据迁移创建任务按钮点击钩子回调
    [CRUD.HOOK.beforeToAdd](crud) {
      this.dialogType = 2
      this.form = crud.form
    },
    // 数据迁移编辑任务按钮点击钩子回调
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.dialogType = 1
      this.form = crud.form
      // 用于编辑功能时获取任务名称
      this.form.validateName = this.form.taskName
      this.taskPkId = form.pkId
      const param = {
        'taskPkId': form.pkId
      }
      const param2 = { size: 500, status: 'ENABLE' }
      dataSourceConnApi.findListByPage(param2).then(data => {
        this.sourceConnList = data.content
        dataTransferList.findListByPage(param).then(data => {
          this.form.tableList = data.content
          this.form.sourceConnPkId = this.form.tableList[0].sourceConnPkId
          this.form.database = this.form.tableList[0].sourceDatabaseName
          this.form.tableList.forEach(item => {
            this.sourceConnList.forEach(item2 => {
              if (item2.pkId === item.sourceConnPkId) {
                item.sourceConnName = item2.sourceName
              }
              if (item2.pkId === item.targetConnPkId) {
                item.targetConnName = item2.sourceName
              }
            })
          })
        })
      }).catch(() => {
        this.crud.notify('获取源/目标服务器失败！', 'error')
      })
    },
    formatterTransferType(val) {
      if (val === 'ExpandNum') {
        return '载数'
      } else if (val === 'DownwardNum') {
        return '下数'
      }
      return '载数'
    },
    toShowDetail(dataTransferTaskPkId) {
      this.dataTransferTaskPkId = dataTransferTaskPkId
      this.showDataTransferLogFlag = true
    },
    // 保存/保存并同步
    saveAndSynchronize() {
      this.crud.submitCU()
      this.crud.notify('数据正在迁移中,请等候', CRUD.NOTIFICATION_TYPE.SUCCESS)
    },
    // 保存
    saveOnly() {
      this.crud.submitCU()
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-transfer-panel {
  width: 250px;
}

::v-deep .bordered-button {
  border: #92dab3 2px;
}

::v-deep .el-transfer__buttons button {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

::v-deep .el-transfer__buttons button:first-child span span {
  margin-left: 0px;
}

.assist {
  min-height: 46px;
  color: #fff;
  background-color: #ff4949;
  border-color: #ff4949;
}
</style>

